package com.njupt.towerssystem.services.impl;

import com.njupt.towerssystem.mapper.ManageInfoMapper;
import com.njupt.towerssystem.services.PowerService;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

@Service
@Transactional
public class PowerServiceImpl implements PowerService {
    private static Logger logger = (Logger) LoggerFactory.getLogger(PowerServiceImpl.class);
    @Resource
    private ManageInfoMapper manageInfoMapper;

    @Override
    public void addNewRule(int ruleid, String rulename) {
        Map mp = new HashMap<>();
        mp.put("ruleid", ruleid);
        mp.put("rulename", rulename);
        manageInfoMapper.insertRule(mp);
    }

    @Override
    public void revokeUserRule(int ruleid, int userid) {
        Map mp = new HashMap<>();
        mp.put("ruleid", ruleid);
        mp.put("userid", userid);
        manageInfoMapper.delRule(mp);
    }

    @Override
    public void grantUserRule(int ruid, int ruleid, int userid) {
        Map mp = new HashMap<>();
        try{
            mp.put("ruid", ruid);
            mp.put("ruleid", ruleid);
            mp.put("userid", userid);
            manageInfoMapper.addRule(mp);
        }catch (Exception ex){
            logger.error("我发现了错误。。。。。"+mp+"\n"+ex.getMessage());
        }
    }
}
